<?php

class syslogistics_api_dlvpoint_update {

    public $apiDescription = "根据送货ID，修改送货点数据";

    public function getParams()
    {
        $return['params'] = array(
            'id' =>['type'=>'string','valid'=>'required', 'description'=>'送货点ID','default'=>'','example'=>'10'],
            'depot_name' =>['type'=>'string','valid'=>'required|max:20', 'description'=>'仓库名称','default'=>'','example'=>'益阳'],
            'depot_bn' =>['type'=>'string','valid'=>'required|max:20', 'description'=>'仓库编码','default'=>'','example'=>'234567'],
            'name' =>['type'=>'string','valid'=>'max:20', 'description'=>'送货点名称','default'=>'','example'=>'商派送货点'],
            'area_id' =>['type'=>'string','valid'=>'', 'description'=>'送货地区ID','default'=>'','example'=>'430000,430000,430102'],
            'addr' =>['type'=>'string','valid'=>'max:50', 'description'=>'送货详细地址','default'=>'','example'=>'桂林路396号2号楼'],
            'tel' =>['type'=>'string','valid'=>'', 'description'=>'送货点联系方式','default'=>'','example'=>'13918765432'],
        );

        return $return;
    }

    public function update($params)
    {
        $objMdldlvpoint = app::get('syslogistics')->model('delivery_point');
        $data = $objMdldlvpoint->getRow('id,name,area,depot_name,depot_bn', ['id'=>$params['id']] );

        if( empty($data) ) throw new LogicException('更新的送货点不存在');

        if( !area::checkArea($params['area_id']) )
        {
            throw new LogicException('请选择完整地区');
        }

        if( trim($params['name']) && $data['name'] != trim($params['name']) )
        {
            $name = $objMdldlvpoint->getRow('id', ['name'=>trim($params['name'])] );
            if( $name && $name['id'] != $params['id']  )
            {
                throw new LogicException('更新的送货点名称重复');
            }
            $updata['name'] = trim($params['name']);
        }

        if( $params['area_id'] )
        {
            $areaIds = explode(',', $params['area_id']);
            if( count($areaIds) == 2 )
            {
                $updata['area_state_id'] = 1;
                $updata['area_city_id'] = $areaIds[0];
                $updata['area_district_id'] = $areaIds[1];
            }
            else
            {
                $updata['area_state_id'] = $areaIds[0];
                $updata['area_city_id'] = $areaIds[1];
                $updata['area_district_id'] = $areaIds[2];
            }

            $updata['area'] = $params['area_id'];
        }
         //根据送货点地址调用百度地图接口获取经纬度
        $address = area::getSelectArea($params['area_id'],'').$params['addr'];
        $ak = "CAd14de4d13fa18ebecbd5ab92af71bc";
        $url = "http://api.map.baidu.com/cloudgc/v1?ret_coordtype=bd09ll&address=$address&ak=$ak";
        $address_data = file_get_contents($url);
        $json_data = json_decode($address_data,true);
        $lat = $json_data['result']['0']['location']['lat'];
        $lng = $json_data['result']['0']['location']['lng'];
        
        $updata['latitude'] = $lat;
        $updata['longitude'] = $lng;

        if(trim($params['depot_name']) )
        {
            $updata['depot_name'] = $params['depot_name'];
        }
        if(trim($params['depot_bn']) )
        {
            $updata['depot_bn'] = $params['depot_bn'];
        }

        if( trim($params['addr'])  )
        {
            $updata['addr'] = $params['addr'];
        }

        if( trim($params['tel']) )
        {
            $updata['tel'] = $params['tel'];
        }

        return $objMdldlvpoint->update($updata, ['id'=>$params['id']]);
    }
}

